local internet__dimingx = fk.CreateSkill({
  name = "internet__dimingx",
  tags = { Skill.Compulsory },
})

-- 回合开始时重置技能使用次数
internet__dimingx:addEffect(fk.RoundStart, {
  anim_type = "special",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and 
           ((player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli") or 
            player:getMark("@evol__universe_will") > 0)
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@internet__dimingx_used-round", 0)
    return true
  end,
})

-- 角色进入濒死状态时触发主要效果
internet__dimingx:addEffect(fk.EnterDying, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    if not ((player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli") or 
            player:getMark("@evol__universe_will") > 0) then 
      return false 
    end
    
    if player:getMark("@internet__dimingx_used-round") >= 2 then
      return false
    end
    
    local room = player.room
    local hook_count = 0
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@internet__tanshex_hook") > 0 then
        hook_count = hook_count + 1
        if hook_count >= 2 then
          return true
        end
      end
    end
    return false
  end,
  
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local hook_players = {}
    for _, p in ipairs(room:getAlivePlayers()) do
      if p:getMark("@internet__tanshex_hook") > 0 then
        table.insert(hook_players, p)
      end
    end
    
    if #hook_players < 2 then return false end
    
    local targets = room:askToChoosePlayers(player, {
      targets = hook_players,
      min_num = 2,
      max_num = #hook_players,
      prompt = "#internet__dimingx-choose",
      skill_name = self.name,
      cancelable = true
    })
    
    if #targets < 2 then return false end
    
    event:setCostData(self, targets)
    return true
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@internet__dimingx_used-round", 1)
    
    local path_players = event:getCostData(self)
    room:sendLog{type = "#DimingPath", from = player.id, arg = #path_players}
    
    local start_player = path_players[1]
    local damage_value = start_player:getLostHp()
    room:sendLog{type = "#DimingDamage", arg = damage_value}
    
    for _, p in ipairs(path_players) do
      if p.dead then goto continue end
      
      local nearest_hook_player = nil
      local min_distance = 999
      
      for _, hook_p in ipairs(room:getAlivePlayers()) do
        if hook_p:getMark("@internet__tanshex_hook") > 0 and hook_p ~= p then
          local n = #room.players
          local clockwise_distance = (hook_p.seat - p.seat + n) % n
          if clockwise_distance == 0 then clockwise_distance = n end
          
          local counter_clockwise_distance = (p.seat - hook_p.seat + n) % n
          if counter_clockwise_distance == 0 then counter_clockwise_distance = n end
          
          local distance = math.min(clockwise_distance, counter_clockwise_distance)
          if distance < min_distance then
            min_distance = distance
            nearest_hook_player = hook_p
          elseif distance == min_distance and math.random(1, 2) == 1 then
            nearest_hook_player = hook_p
          end
        end
      end
      
      if nearest_hook_player then
        room:sendLog{type = "#DimingSwap", from = p.id, to = {nearest_hook_player.id}}
        room:swapSeat(p, nearest_hook_player)
        
        if p == player then
          room:sendLog{type = "#DimingDraw", from = player.id, arg = damage_value}
          player:drawCards(damage_value, self.name)
        else
          room:sendLog{type = "#DimingHurt", from = p.id, arg = damage_value}
          room:damage({
            from = player,
            to = p,
            damage = damage_value,
            damageType = fk.NormalDamage,
            skillName = self.name,
          })
        end
      end
      
      ::continue::
    end
    
    return true
  end,
})

Fk:loadTranslationTable{
  ["internet__dimingx"] = "地鸣",
  [":internet__dimingx"] = "锁定技，共鸣技，每轮限两次，当有角色进入濒死状态时，你可以选择一条由「钩爪残影」组成的闭合路径（即多个连续有「钩爪残影」的角色，至少2个）。令路径内所有角色依次执行：交换位置至与其最近的一个钩爪残影点（若有多个则随机一个），受到X点伤害（X=起始位已损失体力值），若包括你，则该效果于你改为摸X张牌。",
  ["@internet__dimingx_used-round"] = "地鸣",
  ["#internet__dimingx-choose"] = "地鸣：请选择一条由「钩爪残影」组成的闭合路径（至少2个角色）",
  ["#DimingPath"] = "%from 选择了由 %arg 个角色组成的地鸣闭合路径",
  ["#DimingDamage"] = "地鸣伤害值：%arg",
  ["#DimingSwap"] = "%from 与 %to 交换了位置",
  ["#DimingHurt"] = "%from 受到 %arg 点地鸣伤害",
  ["#DimingDraw"] = "%from 摸了 %arg 张牌",
}

return internet__dimingx